package org.example.auth.controller;


import com.example.common.result.UserResult;
import com.example.common.utils.JWTUTils;
import com.example.model.auth.dto.UserDTO;
import com.example.model.auth.dto.UserLoginDTO;
import lombok.extern.slf4j.Slf4j;
import org.example.auth.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@Slf4j
public class UserLoginController {
    @Autowired
    private CustomerService customerService;
    @PostMapping("/fastget/user/login")
    public UserResult getUserByPasswordAndUsername(@RequestBody UserLoginDTO userLoginDTO){
        log.info("{}",userLoginDTO);
        UserDTO user = customerService.getUserByPasswordAndUsername(userLoginDTO);
        if (user!=null){
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("name",user.getName());
            stringObjectHashMap.put("username",user.getUsername());
            stringObjectHashMap.put("id",user.getCustomerId());
            String generate = JWTUTils.generate(stringObjectHashMap);
            user.setToken(generate);
            return UserResult.success(user);
        }else {
            return UserResult.error("用户或者密码错误");
        }

    }
}
